package com.hetang.controller.advice;

import com.hetang.common.StringUtils;
import com.hetang.domain.common.MsgResponse;
import com.hetang.domain.enums.ResponseCodeEnum;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.WebRequest;

/**
 * Created by Duo Duo on 2016/12/23.
 * @ControllerAdvice 控制控制器的全局信息
 */
@ControllerAdvice
public class ExceptionHandlerAdvice {

    @ExceptionHandler(value = Throwable.class)
    @ResponseBody
    public MsgResponse<Object> exception(Exception e, WebRequest request){
        if(StringUtils.isNullOrEmpty(e.getMessage())){
            return new MsgResponse<>(ResponseCodeEnum.EXCEPTION);
        }
       return new MsgResponse<>(ResponseCodeEnum.EXCEPTION,e.getMessage());
    }
    
    @ModelAttribute
    public void addAttributes(Model model){
        model.addAttribute("msg","全局额外信息");
    }
}
